@echo off
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rem  Package:		OurGrid 4.0
rem  Description:	This batch script is used to create configure
rem                 and configure a Virtual Box Virtual Machine.
rem  Copyright (c)  2007 Universidade Federal de Campina Grande 
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if %1.==/?. goto usage
if %3.==. goto usage
goto findself

:findself
set SELF=%~d0%~p0
set SHARED=ourgridshared
goto main

:main
set VMANAGE=%1
set IMAGE=%2
set DISK=%3

set MEM=128
if not %4.==. set MEM=%4

echo Creating Virtual Machine
%VMANAGE% createvm -name %IMAGE% -register
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Unregistering disk (in case already registered)
%VMANAGE% unregisterimage disk %DISK% > nul

	
echo Registering disk
%VMANAGE% registerimage disk %DISK% -type immutable
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Unseting hard drive
%VMANAGE% modifyvm %IMAGE% -hda none
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Reseting hard drive
%VMANAGE% modifyvm %IMAGE% -hda %DISK%
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Setting memory
%VMANAGE% modifyvm %IMAGE% -memory %MEM%
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Optimizing boot time. (STEP 1/3)
%VMANAGE% modifyvm %IMAGE% -bioslogofadein off
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Optimizing boot time. (STEP 2/3)	
%VMANAGE% modifyvm %IMAGE% -bioslogofadeout off
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Optimizing boot time. (STEP 3/3)
%VMANAGE% modifyvm %IMAGE% -biosbootmenu disabled
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Removing network.
%VMANAGE% modifyvm %IMAGE% -nic1 none
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

echo Copying .VirtualBox directory to system profile
xcopy "%HOMEDRIVE%%HOMEPATH%\.VirtualBox" "%SystemRoot%\system32\config\systemprofile\.VirtualBox\" /E/R/Y
if not %ERRORLEVEL% == 0 (
	echo Unable to copy .VirtualBox directory to system profile 1>&2
	exit /B 1
)

echo Starting Image. Please shut it down.
%VMANAGE% startvm %IMAGE%
if not %ERRORLEVEL% == 0 (
	echo Unable to create image 1>&2
	exit /B 1
)

goto eof

:usage
echo usage:
echo Usage $0 ^<VBoxManage location^> ^<Image name^> ^<Virtual disk^> [Virtual machine memory] [-h]
echo where mandatory parameters are:
echo       VBoxManage location: Is the location of VBoxManage command
echo       Image name: Is the name of the virtual machine
echo       Virtual disk: Is the location of the virtual disk to use
echo and the optional parameters are:
echo       Virtual Machine Memory : Amount of memory to allocate to the VM in mega bytes. Default is 128.

:eof